'use strict';

const baseController = require('../../utils/base')

class RoleController extends baseController {
    async index() {
        const result = await this.app.mysql.query('select * from role')
        console.log(result);
        await this.ctx.render(`${this.config.pathname}/role/index`, {
            list: result
        })
    }
    async add() {
        await this.ctx.render(`${this.config.pathname}/role/add`)
    }
    async doAdd() {
        let { title, description } = this.ctx.request.body;
        title = title.trim()
        console.log(title, '888');
        if (title != '') {
            const result = await this.app.mysql.query('INSERT INTO  role (title, description, status, add_time) VALUES (?,?,?,?)', [title, description, 1, await this.service.admin.utlis.getDate()])
            console.log(result, "---");
            await this.success('添加角色成功', `/${this.config.pathname}/role`)
        } else {
            await this.error('角色名称不能为空', `${this.config.pathname}/role/add`)
        }
    }
    async edit() {
        console.log(this.ctx.query);
        const result = await this.app.mysql.query('select * from role where id = ?', [this.ctx.query.id])
        console.log(result[0], "result");
        await this.ctx.render(`${this.config.pathname}/role/edit`, {
            list: result[0],
            id: this.ctx.query.id
        })

        // this.ctx.body = "权限 编辑" + this.ctx.query.id
    }
    async doEdit() {
        try {
            let { title, description, id } = this.ctx.request.body
            const result = await this.app.mysql.query('update role set title = ?,description = ? where id = ?', [title, description, id])
            if (result.protocol41) {
                await this.success('更新角色成功', `/${this.config.pathname}/role`)
            } else {
                await this.error('更新角色失败', `/${this.config.pathname}/role`)
            }
        } catch (error) {
            await this.error('更新角色发生致命错误', `/${this.config.pathname}/role`)
        }
    }
    async del() {
        try {
            let { id } = this.ctx.query
            const result = await this.app.mysql.query('delete from role where id = ?', [id])
            console.log(result, 'result----------------------------------------------');
            if (result.protocol41) {
                await this.success('删除角色成功', `/${this.config.pathname}/role`)
            } else {
                await this.error('删除角色失败', `/${this.config.pathname}/role`)
            }
        } catch (error) {
            await this.error('删除角色发生致命错误', `/${this.config.pathname}/role`)
        }
    }
    async auth() {
        var { id } = this.ctx.query

        //  查询出所以权限
        let result = await this.ctx.model.Access.findAll({
            where: {
                moduleId: 0
            },
            include: { model: this.ctx.model.Access }
        });
        // 回显出现  当前用户有的权限
        let arrRole = await this.app.mysql.query('select access_id from role_access where role_id = ?', id)
        console.log(arrRole);
        var temp = []
        for (let i = 0; i < arrRole.length; i++) {
            temp.push(arrRole[i].access_id);
        }
        console.log(temp);
        result = JSON.parse(JSON.stringify(result))
        for (let i = 0; i < result.length; i++) {
            if (temp.indexOf(result[i].id) != -1) {
                result[i].checked = true
            }
            for (let j = 0; j < result[i].accesses.length; j++) {
                if (temp.indexOf(result[i].accesses[j].id) != -1) {
                    result[i].accesses[j].checked = true
                }
            }
        }
        await this.ctx.render('admin/role/auth', {
            roleId: id,
            authList: result
        })
    }
    async doAuth() {
        var { accessIds, roleId } = this.ctx.request.body
        await this.app.mysql.query('delete from role_access where role_id = ?', roleId)
        for (let i = 0; i < accessIds.length; i++) {
            await this.app.mysql.query('insert into role_access (access_id, role_id) VALUES (?,?)', [accessIds[i], roleId])
        }
        await this.success('授权成功', `/${this.config.pathname}/role/auth?id=${roleId}`)
    }
}

module.exports = RoleController;